﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Group16.Class_Library;
namespace Group16.Controller
{
    public partial class CustomerInfoForm : Form
    {
        private Customer _custom;
        public CustomerInfoForm()
        {
            InitializeComponent();
            Customer = new Customer();
        }
        public Customer Customer
        {
            get { return _custom; }
            set { _custom = value; }
        }
        /// <summary>
        /// Setting for search invoice form 
        /// </summary>
        public void SettingForm()
        {
            if (Customer != null)
            {
                txtName.Text = Customer.Name;
                txtName.BackColor = Color.WhiteSmoke;
                txtName.ReadOnly = true;
                txtAddress.Text = Customer.Address;
                txtAddress.BackColor = Color.WhiteSmoke;
                txtAddress.ReadOnly = true;
                txtPhoneNum.Text = Customer.Phone;
                txtPhoneNum.BackColor = Color.WhiteSmoke;
                txtPhoneNum.ReadOnly = true;
                btnClear.Visible = false;
                btnOk.Visible = false;
            }
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (CheckTextBoxs())
            {
                Customer.Name = this.txtName.Text;
                Customer.Address = this.txtAddress.Text;
                Customer.Phone = this.txtPhoneNum.Text;
                int c = GetCode();
                if (c < 0)
                {
                    return;
                }
                string code = c.ToString();//TODO: incorrect for customer's code
                Customer.Code = code;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                this.lbMessage.Text = "All infomation must be correct!";
            }
        }
        /// <summary>
        /// Return customer's code
        /// </summary>
        /// <returns></returns>
        private int GetCode()
        {
            try
            {
                string comText = "Select Count(Code) from Customer";
                return (int)DataBaseInteraction.ExecuteCommand(comText, 3);
            }
            catch (SqlException se)
            {
                MessageBox.Show("Failed to interact with database " + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
            return -1;
        }
        /// <summary>
        /// Check whether the infomations have been filled
        /// </summary>
        /// <returns></returns>
        private bool CheckTextBoxs()
        {
            return !(String.IsNullOrEmpty(this.txtName.Text) || String.IsNullOrEmpty(this.txtAddress.Text));
        }
        private void txtPhoneNum_Leave(object sender, EventArgs e)
        {
            if (txtPhoneNum.TextLength != 11)
                errorProvider.SetError(txtPhoneNum, "Please check these number!");
        }
        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            Char c = e.KeyChar;
            e.Handled = !(Char.IsLetter(c) || Char.IsControl(c) || c == ' ');
            if (e.Handled)
                Console.Beep();
        }
        private void txtPhoneNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            Char c = e.KeyChar;
            e.Handled = !(Char.IsNumber(c) || Char.IsControl(c));
            if (e.Handled)
                Console.Beep();
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtAddress.Text = "";
            txtName.Text = "";
            txtPhoneNum.Text = "";
            lbMessage.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
